<?php

namespace app\jiyun\controller\warehouse;

use app\common\model\admin\AccountModel;
use app\common\model\base\CountryModel;
use app\common\model\jiyun\warehouse\WarehouseAbroadModel;
use think\admin\Controller;
use think\admin\helper\QueryHelper;

/**
 * 仓库列表
 * @class WarehouseAbroad
 * @package app\jiyun\controller\warehouse
 * @auth shirukun
 * @date 2024/12/04 15:23
 */
class WarehouseAbroad extends Controller
{
    // 不过滤user_id
    public $check_user = false;

    /**
     * 海外仓列表
     * @auth true
     * @menu true
     */
    public function index()
    {
        WarehouseAbroadModel::mQuery()->layTable(function (){
            $this->title = lang('海外仓列表');

            // 公司列表
            if ($this->super) {
                $this->account_list = AccountModel::mk()->where(['status'=> 1])->column('title', 'id');
            }else{
                $this->account_list = [];
            }

        },function (QueryHelper $query){

            $query->order('sort desc');
            $query->like("warehouse_c_name,warehouse_e_name")->equal("status,account_id");

        });
    }

    protected function _index_page_filter(&$data){
        // 国家
        $country = CountryModel::column("coun_c_name","id");
        // 公司列表
        if ($this->super) {
            $account_list = AccountModel::mk()->where(['status' => 1])->column('title', 'id');
        }else{
            $account_list = [];
        }

        foreach ($data as &$item) {
            $item['country_c_name'] = $country[$item['country_id']] ?? "-";
            $item['company_name'] = $account_list[$item['account_id']] ?? "-";
        }
    }

    /**
     * 添加海外仓
     * @auth true
     */
    public function add()
    {
        WarehouseAbroadModel::mForm("form");
    }

    /**
     * 编辑海外仓
     * @auth true
     */
    public function edit()
    {
        WarehouseAbroadModel::mForm("form");
    }

    /**
     * 表单前置回调
     */
    protected function _form_filter(array &$data)
    {
        if ($this->request->isPost()) {
            $rule = [
                "account_id|".lang('公司')    =>  "require|integer",
                "country_id|".lang('国家')    =>  "require|integer",
                "warehouse_c_name|".lang('仓库中文名称')    =>  "require|length:1,50",
                "warehouse_e_name|".lang('仓库英文名称')    =>  "require|length:1,50",
                "code|".lang('仓库编码')    =>  "require|length:1,50",
                "contacts|".lang('仓库联系人')    =>  "require|length:1,50",
                "house_phone|".lang('仓库联系电话')    =>  "require|length:1,50",
                "zip_code|".lang('仓库邮政编码')    =>  "require|length:1,20",
                "email|".lang('邮箱')    =>  "require|length:1,20|email",
                "city|".lang('仓库 / 城市')    =>  "require|length:1,100",
                "district|".lang('仓库 / 洲')    =>  "require|length:1,100",
                "address|".lang('仓库详细地址')    =>  "require|length:1,255",
                "sort|".lang('排序')    =>  "number|length:1,10",
                "volume|".lang('体积')    =>  "float|between:0,999999",
            ];
            $validate = new \think\Validate;

            // 使用 rule 方法指定验证规则
            $validate->rule($rule);
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }
        }else{

            // 国家
            $this->country_list = CountryModel::select();
            // 公司列表
            if ($this->super) {
                $this->account_list = AccountModel::mk()->where(['status'=> 1])->column('title', 'id');
            }else{
                $this->account_list = [];
            }

            if(empty($data['id'])){
                $this->channels = json_encode([],320);
                // 下拉框渠道赋值
                $this->channel_ids_arr = json_encode([],320);
            }else{
                // 渠道列表
//                $this->channels = ChannelModel::mk()->where(['account_id'=>$data['account_id'],'status'=>1])->select()->toArray();
//                $this->channels = json_encode($this->channels,320);
//                // 下拉框渠道赋值
//                $this->channel_ids_arr = explode(',',$data['channel_ids']);
//                $this->channel_ids_arr = json_encode($this->channel_ids_arr,320);
            }
        }
    }

    /**
     * 删除
     * @auth true
     * @menu true
     */
    public function remove()
    {
        WarehouseAbroadModel::mDelete();
    }

    /**
     * 状态
     * @auth true
     * @menu true
     */
    public function state()
    {
        WarehouseAbroadModel::mSave($this->_vali([
            'status.require' => lang('修改状态不能为空'),
            'status.in:1,2'  => lang('修改状态范围无效'),
        ]));
    }
}
